package definite.Guide.Book.JCheckBox;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

import definite.Guide.Book.ToggleButton.DiamondIcon;

/*
 * Ham setSelectedIcon(Icon..) : thay doi icon khi checkbox duoc selected 
 * AbstractButton la Class Cha cua Class Button va Menu .
 */

public class IconCheckBoxSample {

	private ImageIcon checkIcon = new ImageIcon("plus.gif");
	private ImageIcon uncheckIcon = new ImageIcon("minus.gif");

	public void paintIcon(Component component, Graphics g, int x, int y) {

		AbstractButton abstractBtn = (AbstractButton) component;
		ButtonModel btnModel = abstractBtn.getModel();
		g.translate(x, y);
		ImageIcon imageIcon = btnModel.isSelected() ? checkIcon : uncheckIcon;
		Image image = imageIcon.getImage();
		g.drawImage(image, 0, 0, component);
		g.translate(-x, -y);

	}

	public int getIconWidth() {
		return 20;
	}

	public int getIconHeight() {
		return 20;
	}

	public static void main(String[] args) {

		JFrame frame = new JFrame("Iconizing CheckBox");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Icon checked = new DiamondIcon(Color.BLACK, true);
		Icon unchecked = new DiamondIcon(Color.BLACK, false);

		JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
		aCheckBox1.setSelectedIcon(checked);
		JCheckBox aCheckBox2 = new JCheckBox("CalZone");
		aCheckBox2.setIcon(unchecked);
		aCheckBox2.setSelectedIcon(checked);
		JCheckBox aCheckBox4 = new JCheckBox("Stuff Crush", checked);
		aCheckBox4.setSelectedIcon(new ImageIcon("plus.gif"));

		frame.setLayout(new GridLayout(0, 1));
		frame.setSize(300, 200);
		frame.add(aCheckBox1);
		frame.add(aCheckBox2);
		frame.add(aCheckBox4);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}
